<template>
	<view class="content">
		<u--form labelPosition="left" labelWidth="70" :model="userInfo" :rules="rules" ref="form1">
			<u-form-item label="客户名称:" prop="nickname">
				<u--input
				    v-model="userInfo.nickname" placeholder="请输入名称"
				    border="surround"
				  ></u--input>
			</u-form-item>
			<u-form-item label="手机号码:" prop="mobile">
				<u--input
				    v-model="userInfo.mobile" placeholder="请输入名称"
				    border="surround"
				  ></u--input>
			</u-form-item>
			<u-form-item label="性别:">
				<u-radio-group 
				    v-model="userInfo.gender"
				    placement="row">
					<u-radio name="0"  label="保密" style="margin-right: 10px;"></u-radio>
					<u-radio name="1" label="男" style="margin-right: 10px;"></u-radio>
					<u-radio name="2" label="女"></u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="成交状态:">
				<u-radio-group 
				    v-model="userInfo.deal"
				    placement="row">
					<u-radio name="0"  label="未成交" style="margin-right: 10px;"></u-radio>
					<u-radio name="1" label="已成交" style="margin-right: 10px;"></u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="客户来源:" prop="sourceid" @click="isSource = true">
				<u--input v-model="userInfo.name" disabled disabledColor="#ffffff" placeholder="请选择回访客户" suffixIcon="arrow-down">
				</u--input>
			</u-form-item>
			<u-form-item label="地区选择:" prop="region" @click="RegionShow = true">
				<pick-regions :defaultRegion="userInfo.region" @getRegion="handleGetRegion">
					<u--input v-model="userInfo.region_text" disabled disabledColor="#ffffff" placeholder="请选择地区">
					</u--input>
					<u-icon slot="right" name="arrow-right">
					</u-icon>
				</pick-regions>
			</u-form-item>
		</u--form>
		<u-button @click="submit" color="#19BC9C">添加</u-button>
		<!-- 来源数据 -->
		<u-picker :show="isSource" :columns="sourcelist" keyName="name" @cancel="isSource = false" @confirm="confirm"></u-picker>
		
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	//引入第三方的地区插件
	import pickRegions from '@/components/pick-regions/pick-regions.vue'
	export default {
		components:{
			pickRegions,
		},
		data() {
			return {
				show: false,
				userInfo: {
					nickname: '',
					name: '',
					mobile: '',
					gender: '0',
					deal: '0',
					region: '',
					region_text: '',
					sourceid: null
				},
				rules: {
					'nickname': {
						type: 'string',
						required: true,
						message: '请输入客户名称',
						trigger: ['blur', 'change']
					},
				},
				LoginUser: {},
				isSource: false,
				sourcelist: []
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.form1.setRules(this.rules)
			this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {};
			this.$u.auth.check()
			this.getsourcelistdata()
		},
		methods: {
			async getsourcelistdata(){
				let result = await this.$u.api.business.sourceIndex()
				if (result.code == 1) {
					this.sourcelist.push(result.data)
				}
			},
			// 回调参数为包含columnIndex、value、values
			confirm(e) {
				this.isSource = false
				this.userInfo.sourceid = e.value[0].id
				this.userInfo.name = e.value[0].name
			},
			handleGetRegion(val){
				let [province,city,district] = val
				this.userInfo.region_text = province.name + "-" + city.name + "-" + district.name
				// this.userInfo.region = district.code
			},
			submit(){
				this.$refs.form1.validate().then(async(res) => {
					let data = {
						nickname: this.userInfo.nickname,
						mobile: this.userInfo.mobile,
						gender: this.userInfo.gender,
						sourceid: this.userInfo.sourceid,
						deal: this.userInfo.deal,
						region: this.userInfo.region,
						adminid:this.LoginUser.id
					}
					let result = await this.$u.api.business.privateseaAdd(data)
					if(result.code == 1){
						this.$refs.uToast.show({
							type: 'success',
							message: result.msg,
							complete() {
								uni.navigateTo({
									url: '/pages/business/privatesea/index'
								});
							}
						})
					}  else {
						this.$refs.uToast.show({
							type: 'error',
							message: result.msg
						})
					}
				}).catch(errors => {})
				
			}
		}
	}
</script>

<style>
	.content{
		/* width: 90%; */
		height: 100vh;
		/* margin: 0 auto; */
	}
	uni-picker{
		width: 100%;
	}
</style>
